package cn.aib.educenter.controller;


import cn.aib.commonutils.JwtUtils;
import cn.aib.commonutils.R;
import cn.aib.commonutils.vo.UcenterMemberVo;
import cn.aib.commonutils.vo.UserInfoVo;
import cn.aib.educenter.entity.UcenterMember;
import cn.aib.educenter.entity.vo.RegisterVo;
import cn.aib.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-16
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;
    /**
     *  登录功能：
     *  方法名：login
     *  参数：UcenterMember menber
     *  返回值：R
     */
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member) {

        //调用memberService完成登录业务,并返回token
        String token = memberService.login(member);

        return R.ok().data("token", token);
    }

    /**
     *  注册功能：
     *  方法名：regist
     *  参数：RegisterVo registerVo
     *  返回值：R
     */
    @PostMapping("regist")
    public R regist(@RequestBody RegisterVo registerVo) {

        //调用memberService完成登录业务,并返回token
        memberService.regist(registerVo);

        return R.ok();
    }

    /**
     *  获取会员信息：
     *  方法名：getUserInfoByToken
     *  参数：HttpServletRequest request
     *  返回值：R
     */
    @GetMapping("getUserInfo")
    public R getUserInfoByToken(HttpServletRequest request) {

        //根据token取用户信息
        UserInfoVo user = JwtUtils.getMemberInfoByJwtToken(request);
        System.out.println("user-------:"+user);
        //返回
        return R.ok().data("user",user);
    }


    /**
     *  获取会员信息：
     *  方法名：getUserInfoByToken
     *  参数：HttpServletRequest request
     *  返回值：R
     */
    @GetMapping("getMemberById/{memberId}")
    public UcenterMemberVo getMemberById(@PathVariable("memberId") String memberId) {

        UcenterMember member = memberService.getById(memberId);

        UcenterMemberVo memberVo = new UcenterMemberVo();

        BeanUtils.copyProperties(member,memberVo);

        //返回
        return memberVo;
    }

    /**
     *  获取会员信息：
     *  方法名：getUserInfoByToken
     *  参数：HttpServletRequest request
     *  返回值：R
     */
    @GetMapping("getUserAllInfo")
    public R getUserAllInfo(HttpServletRequest request) {
        //根据token取用户信息
        String id = JwtUtils.getMemberIdByJwtToken(request);

        UcenterMember member = memberService.getById(id);

        //返回
        return R.ok().data("member",member);

    }

    /**
     *  获取某天的注册人数
     *  方法名：getRegisterFrom
     *  参数：String date
     *  返回值：Integer
     */
    @GetMapping("getRegisterFrom/{date}")
    public Integer getRegisterFrom(@PathVariable("date") String date) {

        Integer count = memberService.getRegisterFrom(date);

        //返回
        return count;
    }
}

